---@class UIStoryMemoryList : UIBase
UIStoryMemoryList = UIBase.New(PanelResNames.UIStoryMemoryList)
local t = UIStoryMemoryList

function t.Init(id)
    t.id = id
    UIBase.Init(t)
end

function t:OnCreate()
    self.btnClose = self:GetChild("btnClose")
    self.itemList = self:GetList("list")
    self.memoryItemList = self:GetList("memoryList")
    self.contentText = self:GetChild("content")
    self.ctrlChapter = self:GetController("chapter")
    self.contentText.text = ""
    self:SetDynamicList(self.itemList,self.ItemRender)
    self:SetDynamicList(self.memoryItemList,self.MemoryItemRender)
    self:RegisterOnClick(self.btnClose,self.OnBack)
end

function t:OnEnable()
    self.showFirst = true
    self:InitChapter(t.id)
end
function t:OnDisable()
end
function t:OnDestroy()
end

--初始化章节
function t:InitChapter(sendID)

    local CrystalDic=G.dataTable["t_memorycrystal"][tostring(sendID)]
    if CrystalDic then
        self.memoryDataList = decodeJsonStrToTabel(CrystalDic.f_CrystalContent);
        self.memoryItemList.numItems = #self.memoryDataList
    else
        logError("找不到t_memorycrystal  id:"..sendID)
    end


end
function t:ItemRender(index,go)
    local data = self.storyDataList[index]
    local headGo = go:GetChild("head")
    local headIcon= headGo:GetChild("icon")
    local talkName= go:GetChild("name")
    local talkinfo= go:GetChild("info")
    local Replay=go:GetChild("btnPlay")
    if(data["Chap"].f_StoryType==2 or data["Chap"].f_StoryType==3)then
        local tempStory=G.dataTable["t_story"][ tostring(data["StoryID"])];
        if not string.IsNilOrEmpty(tempStory.f_storyChose) then
            headGo.visible = false
            talkName.text = "";
            talkinfo.text = getLanguage("MemoryTips_003");
        else
            headGo.visible = data["Chap"].f_StoryType == 2

            headIcon.url = EnumConst.AtlasPath.ICONAtlas..tempStory.f_icon
            if(tempStory.f_name~="")then
                talkName.text=getLanguage(tempStory.f_name)..":";
            else
                talkName.text="";
            end
            talkinfo.text=getLanguage(tempStory.f_StoryContent);
        end

    else
        headGo.visible = false
        talkName.tex="";
        if(data["Chap"].f_StoryType==1)then
            talkinfo.text=getLanguage("MemoryTips_001");
        end
        if(data["Chap"].f_StoryType==4)then
            talkinfo.text=getLanguage("MemoryTips_002");
        end
    end
    Replay.onClick:Clear()
    Replay.onClick:Add(function() self:PlayStory(data) end )
end
function t:MemoryItemRender(index,go)
    local data = self.memoryDataList[index]
    local id = tostring(data)
    local cfg =G.dataTable["t_storymain"][id]
    if not cfg then
        logError("找不到t_storymain   id："..id)
        return
    end
    local headIcon=go:GetChild("head"):GetChild("icon")
    local nameText = go:GetChild("title")
    nameText.text = getLanguage(cfg.f_Name)
    headIcon.url = "ui://UIStoryMemeryList/"..cfg.f_StoryCrystal
    go.onClick:Clear()
    go.onClick:Add(function()
        self:ShowAllStory(cfg.f_ChapterID)

    end)
    if self.showFirst then
        self.ctrlChapter.selectedIndex = 0
        self:ShowAllStory(cfg.f_ChapterID);
        self.showFirst=false;
    end
end
function t:ShowAllStory(thisChapID)
    local StoryDic=G.dataTable["t_storymain"]
    local table_banch={}
    for k,v in pairsByKeys(StoryDic) do
        if(v.f_ChapterID==thisChapID)then
            if(v.f_StoryType==3 or v.f_StoryType==2)then
                local contentTab=decodeJsonStrToTabel(v.f_contentID)
                if type(contentTab) == "table" then
                    for i = 1, #contentTab do
                        local singleCont={}
                        singleCont["Chap"]=v;
                        singleCont["StoryID"]=contentTab[i];
                        table.insert(table_banch,singleCont);
                    end
                else
                    logError("json格式错误："..v.f_contentID)
                end
            else
                local singleStory={}
                singleStory["Chap"]=v;
                table.insert(table_banch,singleStory);
            end
        end
    end
    self.storyDataList = table_banch
    self.itemList.numItems = #self.storyDataList
end
function t:PlayStory(PlayItem)
    logicMgr.StoryMgr.RecordStory(PlayItem["Chap"].f_StoryID)
end
return t